package kr.or.kead.daegu.global.InputUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import kr.hkit.framework.dao.CloseDaoUtil;
import kr.hkit.framework.dao.LoadPropertyDAO;
import kr.hkit.framework.inputPanel.InputCmbPanel;
import kr.hkit.framework.inputPanel.InputTxtPanel;
import kr.or.kead.daegu.global.Config;
import kr.or.kead.daegu.global.DTO.Business;
import kr.or.kead.daegu.global.DTO.Emp;
import kr.or.kead.daegu.global.DTO.Product;
import kr.or.kead.daegu.global.Service.DbDao;

public class InputBaseUI extends JPanel implements ActionListener {

	private JButton btnDel;
	private JButton btnSearch;
	private JButton btnSave;
	
	private InputTxtPanel[] inputFields;
	private InputCmbPanel panel_combo;
	
	private JPanel panel;
	private int fieldflag;
	private int comboFlag;
	private DbDao dao;
	private Connection con ; 
	
	
	public InputBaseUI(int fieldflag, int comboFlag) {
		this.fieldflag = fieldflag;
		this.comboFlag = comboFlag;
		this.con = LoadPropertyDAO.getConnection();
		dao = new DbDao(con);
		initialize();
	}
	
	private void initialize() {
		setPreferredSize(new Dimension(300, 200));
		
		setLayout(new BorderLayout(0, 0));
		panel = new JPanel();
		add(panel, BorderLayout.CENTER);
		
		inputFields = new InputTxtPanel[Config.INPUT_FIELD_NAME[fieldflag].length];
		
		int len = Config.INPUT_FIELD_NAME[fieldflag].length+(comboFlag>-1?1:0);
		panel.setLayout(new GridLayout(len, 0, 0, 0));
		
		for(int i=0; i<Config.INPUT_FIELD_NAME[fieldflag].length;i++){
			inputFields[i] = new InputTxtPanel(Config.INPUT_FIELD_NAME[fieldflag][i]);
			panel.add(inputFields[i]);	
		}
		
		if (comboFlag>-1){
			panel_combo = new InputCmbPanel(Config.INPUT_COMBO_NAME[comboFlag]);
			panel_combo.addItems(Config.INPUT_COMBO_LIST[comboFlag]);
			panel.add(panel_combo);
		}
		
		JPanel panel_1 = new JPanel();
		add(panel_1, BorderLayout.SOUTH);
		
		btnSave = new JButton("저장");
		btnSave.addActionListener(this);
		panel_1.add(btnSave);
		
		btnDel = new JButton("삭제");
		btnDel.addActionListener(this);
		panel_1.add(btnDel);
		
		btnSearch = new JButton("검색");
		btnSearch.addActionListener(this);
		panel_1.add(btnSearch);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnSave) {
			btnSaveActionPerformed(e);
		}
		if (e.getSource() == btnSearch) {
			btnSearchActionPerformed(e);
		}
		if (e.getSource() == btnDel) {
			btnDelActionPerformed(e);
		}
	}
	
	protected void clearTxt(){
		for(int i=0; i<Config.INPUT_FIELD_NAME[fieldflag].length;i++){
			inputFields[i].setTfValue("");
		}
		if (panel_combo!=null)
			panel_combo.setSelectItem(Config.INPUT_COMBO_LIST[comboFlag][0]);
	}
	
	protected void btnDelActionPerformed(ActionEvent e) {
		try {
			switch(fieldflag){
			case 0:
				Emp emp = new Emp(inputFields[0].getTfValue().getText());
				dao.selectCodeByCode(emp);
				dao.deleteItem(emp);
				break;
			case 1:
				Product product = new Product(inputFields[0].getTfValue().getText());
				dao.selectCodeByCode(product);
				dao.deleteItem(dao.selectCodeByCode(product));
				break;
			case 2:
				Business business = new Business(inputFields[0].getTfValue().getText());
				dao.selectCodeByCode(business);
				dao.deleteItem(business);
				break;
			}
			JOptionPane.showMessageDialog(null,"삭제 성공");
			clearTxt();
		} catch (Exception e1) {
			JOptionPane.showMessageDialog(null,"삭제 불가");
		} finally {
			CloseDaoUtil.close(con);
		}
	}
	
	protected void btnSearchActionPerformed(ActionEvent e) {
		try {
			switch(fieldflag){
			case 0:
				Emp emp = dao.selectCodeByCode(new Emp(inputFields[0].getTfValue().getText()));
				inputFields[0].setTfValue(emp.getCode());
				inputFields[1].setTfValue(emp.getName());
				panel_combo.setSelectItem(emp.getGrade());
				break;
			case 1:
				Product product = dao.selectCodeByCode(new Product(inputFields[0].getTfValue().getText()));
				inputFields[0].setTfValue(product.getpCode());
				inputFields[1].setTfValue(product.getpName());
				inputFields[2].setTfValue(String.valueOf(product.getRegprice()));
				inputFields[3].setTfValue(String.valueOf(product.getPrimeprice()));
				break;
			case 2:
				Business business = dao.selectCodeByCode(new Business(inputFields[0].getTfValue().getText()));
				inputFields[0].setTfValue(business.getbCode());
				inputFields[1].setTfValue(business.getbName());
				panel_combo.setSelectItem(business.getbGrade());
				break;
			}
		} catch (Exception e1) {
			JOptionPane.showMessageDialog(null,"검색 불가");
			inputFields[0].requestFocus();
		} finally {
			CloseDaoUtil.close(con);
		}
	}
	
	protected void btnSaveActionPerformed(ActionEvent e) {
		Emp emp = null;
		Product product=null;
		Business business = null;
		try {
			
			switch(fieldflag){
			case 0:
				emp = new Emp(inputFields[0].getTfValue().getText(), inputFields[1].getTfValue().getText(), panel_combo.getSelectItem());
				dao.insertItem(emp);
				break;
			case 1:
				product = new Product(inputFields[0].getTfValue().getText(), inputFields[1].getTfValue().getText(), 
						Integer.parseInt(inputFields[2].getTfValue().getText()), Integer.parseInt(inputFields[3].getTfValue().getText()));
				dao.insertItem(product);
				break;
			case 2:
				business = new Business(inputFields[0].getTfValue().getText(), inputFields[1].getTfValue().getText(), panel_combo.getSelectItem());
				dao.insertItem(business);
				break;
			}
			JOptionPane.showMessageDialog(null, "저장 완료");
			clearTxt();
		} catch (Exception e1) {
			JOptionPane.showMessageDialog(null, "덮어씁니다");
			try {
				if (e1.getMessage().equals("1062E")){
					dao.updateItem(emp);
				}else if (e1.getMessage().equals("1062P")){
					dao.updateItem(product);
				}else if (e1.getMessage().equals("1062B")){
					dao.updateItem(business);
				}
				clearTxt();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		} finally {
			CloseDaoUtil.close(con);
		}
	}
	
}
